import 'package:enterprise_v6_app/app/components/custom_modele/custom_card.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_dropdown_selection.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_gradient_line.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/data/entities/application/major_hazard/hazard_instrument_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/major_hazard/major_hazard_controller.dart';
import 'package:enterprise_v6_app/app/modules/application/major_hazard/monitor/monitor_controller.dart';
import 'package:enterprise_v6_app/app/modules/application/major_hazard/monitor/monitor_type.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';

// #FD6B6B
const _transculentBg = Color.fromARGB(46, 253, 107, 107);

class HazardMonitorPage extends StatelessWidget {
  HazardMonitorPage({required this.type, super.key});

  final HazardMonitorType type;

  final controller = Get.put(HazardMonitorController());
  MajorHazardController get hazardController => Get.find<MajorHazardController>();

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: controller,
      builder: (_) {
        return Scaffold(
          body: Container(
            padding: EdgeInsets.only(left: 16.w, right: 16.w, top: 14.w),
            alignment: Alignment.topCenter,
            color: const Color(0xffF5F5F6),
            child: Column(
              children: <Widget>[
                GetBuilder(
                    init: hazardController,
                    builder: (_) {
                      final selected = hazardController.selectedHazard;
                      return CustomDropdownSelection(
                        text: selected?.riskObjectName,
                        hintText: '选择重大风险源',
                        onCallBack: () => hazardController.showHazardSelect(controller.getData),
                      );
                    }),
                AppGaps.vGap10,
                _buildMonitors(),
              ],
            ),
          ),
        );
      },
    );
  }

  Widget _buildMonitors() {
    final items = controller.instruments;
    if (hazardController.selectedHazard == null) {
      return const Expanded(child: Center(
        child: Text('请选择重大危险源'),
      ));
    }
    return Expanded(
      child: ListView.builder(
        itemCount: items.length,
        itemBuilder: (context, index) {
          final item = items[index];
          return Padding(
            padding: EdgeInsets.symmetric(vertical: 8.w),
            child: () {
              switch (type) {
                case HazardMonitorType.camera:
                  return _buildCamera(item);
                case HazardMonitorType.dcs:
                  return _buildGDS(item);
                case HazardMonitorType.gds:
                  return _buildGDS(item);
              }
            }(),
          );
        },
      ),
    );
  }

  Widget _buildGDS(HazardInstrumentEntity data) {
    return CustomCard(
      onTap: () => Get.toNamed(AppRoutes.majorHazardMonitorCurvePage, arguments: {
        'equipCode': data.equipCode,
        'equipName': data.equipName,
        'data': data,
      }),
      padding: EdgeInsets.zero,
      titleWidget: Padding(
        padding: EdgeInsets.all(14.w),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              children: [
                SvgPicture.asset(AppImages.icon_title),
                AppGaps.hGap10,
                SizedBox(
                  width: 140.w,
                  child: Text(
                    data.equipName ?? '未知',
                    overflow: TextOverflow.clip,
                    style: TextStyle(fontSize: 15.w, fontWeight: FontWeight.w500),
                  ),
                ),
              ],
            ),
            Row(
              children: [
                SvgPicture.asset(
                  AppImages.icon_history_curve,
                  height: 16.w,
                  width: 16.w,
                ),
                AppGaps.hGap2,
                Text(
                  '历史曲线图',
                  style: TextStyle(fontSize: 12.w, color: AppColors.font_color_blue),
                )
              ],
            ),
          ],
        ),
      ),
      divider: CustomCard.builtinDivider,
      content: Padding(
        padding: EdgeInsets.only(bottom: 16.w, left: 16.w, right: 16.w),
        child: Column(
          children: data.hazardTargetVOList?.map(_buildDetail).toList() ?? [],
        ),
      ),
    );
  }

  Widget _buildCamera(HazardInstrumentEntity data) {
    return CustomCard(
      onTap: () => Get.toNamed(
        AppRoutes.majorHazardMonitorCameraPage,
        arguments: {
          'url': data.manLinkAddress,
        },
      ),
      padding: EdgeInsets.all(4.w),
      content: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          SizedBox(
            height: 80.w,
            width: 140.w,
            child: Stack(
              alignment: Alignment.center,
              children: [
                Container(
                  decoration: BoxDecoration(
                    color: Colors.cyan,
                    borderRadius: BorderRadius.circular(8.w),
                  ),
                ),
                SvgPicture.asset(
                  AppImages.icon_media_play,
                  height: 20.w,
                  width: 20.w,
                ),
              ],
            ),
          ),
          AppGaps.hGap10,
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              SizedBox(
                width: 120.w,
                child: Text(
                  data.equipName ?? '未知',
                  style: TextStyle(fontSize: 15.w, fontWeight: FontWeight.w500),
                ),
              ),
              AppGaps.vGap6,
              Container(
                decoration: BoxDecoration(
                  color: _transculentBg,
                  borderRadius: BorderRadius.circular(4.w),
                ),
                padding: EdgeInsets.symmetric(horizontal: 14.w, vertical: 4.w),
                child: const Text('未知'),
              ),
              AppGaps.vGap6,
              // if (exception != null)
              //   Row(
              //     children: [
              //       SvgPicture.asset(
              //         AppImages.icon_red_triangle,
              //         height: 8.w,
              //         width: 8.w,
              //       ),
              //       AppGaps.hGap2,
              //       Text(
              //         exception,
              //         style: TextStyle(fontSize: 12.w, color: Colors.grey),
              //       )
              //     ],
              //   ),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildDetailIcon(String? targetName) {
    switch (targetName) {
      case '液位':
        return SvgPicture.asset(AppImages.icon_water);
      case '压力':
        return SvgPicture.asset(AppImages.icon_gas);
      case '温度':
        return SvgPicture.asset(AppImages.icon_temp_indicator);
      default:
        return SvgPicture.asset(AppImages.icon_water);
    }
  }

  /// 高高报（hh）高报（hi）低报（lo）低低报（ll）信号量报警（si）正常（no）
  Widget _buildDetailStatus(String? status) {
    switch (status) {
      case 'hh':
        return Text(
          '高高报',
          style: TextStyle(fontSize: 12.w, color: Colors.red),
        );
      case 'hi':
        return Text(
          '高报',
          style: TextStyle(fontSize: 12.w, color: Colors.red),
        );
      case 'lo':
        return Text(
          '低报',
          style: TextStyle(fontSize: 12.w, color: Colors.red),
        );
      case 'll':
        return Text(
          '低低报',
          style: TextStyle(fontSize: 12.w, color: Colors.red),
        );
      case 'si':
        return Text(
          '信号量报警',
          style: TextStyle(fontSize: 12.w, color: Colors.red),
        );
      case 'no':
        return Text(
          '正常',
          style: TextStyle(fontSize: 12.w, color: Colors.green),
        );
      default:
        return Text(
          '未知',
          style: TextStyle(fontSize: 12.w, color: Colors.grey),
        );
    }
  }

  Widget _buildDetail(HazardInstrumentHazardTargetVOList item) {
    return Padding(
      padding: EdgeInsets.only(top: 16.w),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Row(
            children: [
              _buildDetailIcon(item.targetName),
              Text(
                '${item.targetTypeDicName ?? '未知'}：',
                style: TextStyle(fontSize: 13.w, color: AppColors.font_color_gray),
                textAlign: TextAlign.start,
              ),
              Text(
                '${item.lastValue?.toString() ?? '未知'} ${item.unit ?? ''}',
                style: TextStyle(fontSize: 12.w, color: AppColors.font_color_black),
                textAlign: TextAlign.end,
              ),
            ],
          ),
          Row(
            children: [
              CustomGradientLine2(
                width: 82.w,
                height: 2.w,
              ),
              AppGaps.hGap2,
              _buildDetailStatus(item.targetStatus),
            ],
          ),
        ],
      ),
    );
  }
}
